<?php
include_once('./common.php');
include_once(S_ROOT . '/data/house_config.php');
include_once(S_ROOT . '/house/function.php');

if(is_file('data/house_install.lock')) showmessage('出错了！已经安装过了，若想重新安装，请删除data/house_install.lock！', 'space.php', 3);

$charset = str_replace('-', '', $_SC['charset']);

$extend = $_SGLOBAL['db']->version() > '4.1' ? " DEFAULT CHARSET={$charset}" : "";

$sql[] = "DROP TABLE IF EXISTS `uchome_app_love_action`;";
$sql[] = "
CREATE TABLE IF NOT EXISTS `uchome_app_love_action` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `name` varchar(20) default '',
  `w_l` varchar(100) default '',
  `w_r` varchar(100) default '',
  `leval` smallint(6) unsigned NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `leval` (`leval`)
) ENGINE=MyISAM {$extend};";

$sql[] = "
INSERT INTO `uchome_app_love_action` (`id`, `name`, `w_l`, `w_r`, `leval`) VALUES
(1, '散步', '邀请', '一起到门外的草地上散步', 0),
(2, '送可乐', '给', '一桶冰镇可乐', 0),
(3, '打扫卫生', '和', '两个人一起动手，把屋子整理的十分干净', 0),
(4, '放泻药', '在', '的水杯里下了泻药，够坏的TA', 0),
(5, '泰式按摩', '让', '给自己做了一套全身泰式按摩', 0),
(6, '倒垃圾', '让', '把家里的垃圾倒出去', 0),
(7, '拖地板', '让', '使劲的拖地板，家里好明亮了啊', 0),
(8, '装鬼吓TA', '好坏啊，竟然装鬼吓', '', 0),
(9, '修水管', '自己不会修，不得不叫', '去修水管，浪费水可不是个好习惯', 0),
(10, '做饭', '不会做饭，不得以让', '去做饭给TA吃', 0);";

$sql[] = "DROP TABLE IF EXISTS `uchome_app_love_gift`;";
$sql[] = "
CREATE TABLE IF NOT EXISTS `uchome_app_love_gift` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `gid` smallint(6) unsigned NOT NULL default '0',
  `uid` int(10) unsigned NOT NULL default '0',
  `username` varchar(20) NOT NULL default '',
  `uid_2` int(10) unsigned NOT NULL default '0',
  `username_2` varchar(20) NOT NULL default '',
  `dateline` int(10) unsigned NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `uid_2` (`uid_2`),
  KEY `uid` (`uid`)
) ENGINE=MyISAM {$extend};";

$sql[] = "DROP TABLE IF EXISTS `uchome_app_love_gift_kind`;";
$sql[] = "
CREATE TABLE IF NOT EXISTS `uchome_app_love_gift_kind` (
  `gid` smallint(6) unsigned NOT NULL auto_increment,
  `name` varchar(30) NOT NULL default '',
  `price` smallint(6) unsigned NOT NULL default '0',
  `thick` smallint(6) unsigned NOT NULL default '0',
  `msg` varchar(10) default '',
  PRIMARY KEY  (`gid`)
) ENGINE=MyISAM {$extend};";

$sql[] = "
INSERT INTO `uchome_app_love_gift_kind` (`gid`, `name`, `price`, `thick`, `msg`) VALUES
(1, '玫瑰', 200, 20, '枝'),
(2, '头梳', 300, 30, '把'),
(3, '镜子', 500, 50, '面'),
(4, '口红', 3000, 300, '支'),
(5, '化装盒', 100, 10, '个'),
(6, '梳妆台', 5000, 500, '个');";

$sql[] = "DROP TABLE IF EXISTS `uchome_app_love_history`;";
$sql[] = "
CREATE TABLE IF NOT EXISTS `uchome_app_love_history` (
  `hid` int(10) unsigned NOT NULL auto_increment,
  `uid_1` int(10) unsigned NOT NULL default '0',
  `username_1` varchar(20) NOT NULL default '',
  `uid_2` int(10) unsigned NOT NULL default '0',
  `username_2` varchar(20) NOT NULL default '',
  `dateline` int(10) unsigned NOT NULL default '0',
  `focus` int(10) unsigned NOT NULL default '0',
  `last_time` int(10) unsigned NOT NULL default '0',
  `last_get_time` int(10) unsigned NOT NULL default '0',
  `status` tinyint(1) unsigned NOT NULL default '0',
  `thick` int(10) unsigned NOT NULL default '0',
  PRIMARY KEY  (`hid`),
  KEY `uid_1` (`uid_1`),
  KEY `uid_2` (`uid_2`),
  KEY `status` (`status`)
) ENGINE=MyISAM {$extend};";

$sql[] = "DROP TABLE IF EXISTS `uchome_app_love_house`;";
$sql[] = "
CREATE TABLE IF NOT EXISTS `uchome_app_love_house` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `cid` int(10) unsigned NOT NULL default '0',
  `uid` int(10) unsigned NOT NULL default '0',
  `username` varchar(20) NOT NULL default '',
  `name` varchar(20) NOT NULL default '',
  `status` tinyint(1) unsigned NOT NULL default '1',
  `price` int(11) unsigned NOT NULL default '0',
  `last_time` int(11) unsigned NOT NULL default '0',
  `sent_uid` int(10) unsigned NOT NULL default '0',
  `sent_username` varchar(20) NOT NULL default '',
  `sent_dateline` int(10) unsigned NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `status` (`status`)
) ENGINE=MyISAM {$extend};";

$sql[] = "DROP TABLE IF EXISTS `uchome_app_love_house_kind`;";
$sql[] = "
CREATE TABLE IF NOT EXISTS `uchome_app_love_house_kind` (
  `cid` int(10) unsigned NOT NULL auto_increment,
  `title` varchar(20) NOT NULL default '',
  `buy` mediumint(9) unsigned NOT NULL default '0',
  `goods_money` smallint(6) unsigned NOT NULL default '0',
  PRIMARY KEY  (`cid`)
) ENGINE=MyISAM {$extend};";

$sql[] = "
INSERT INTO `uchome_app_love_house_kind` (`cid`, `title`, `buy`, `goods_money`) VALUES
(1, '平房', 10000, 100),
(2, '公寓', 50000, 500),
(3, '楼房', 100000, 1000),
(4, '别墅', 500000, 5000),
(5, '豪宅', 2000000, 50000);";

$sql[] = "DROP TABLE IF EXISTS `uchome_app_love_invite`;";
$sql[] = "
CREATE TABLE IF NOT EXISTS `uchome_app_love_invite` (
  `uid` int(10) unsigned NOT NULL default '0',
  `fuid` int(10) unsigned NOT NULL default '0',
  `status` tinyint(1) unsigned NOT NULL default '0',
  UNIQUE KEY `uid` (`uid`,`fuid`)
) ENGINE=MyISAM {$extend};";

$sql[] = "DROP TABLE IF EXISTS `uchome_app_love_love_action`;";
$sql[] = "
CREATE TABLE IF NOT EXISTS `uchome_app_love_love_action` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `hid` int(10) unsigned NOT NULL default '0',
  `uid` int(10) unsigned NOT NULL default '0',
  `action_id` int(10) unsigned NOT NULL default '0',
  `dateline` int(10) unsigned NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `hid` (`hid`),
  KEY `uid` (`uid`)
) ENGINE=MyISAM {$extend};";

$sql[] = "DROP TABLE IF EXISTS `uchome_app_love_news`;";
$sql[] = "
CREATE TABLE IF NOT EXISTS `uchome_app_love_news` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `uid` int(10) unsigned NOT NULL default '0',
  `message` varchar(250) default '',
  `dateline` int(10) unsigned NOT NULL default '0',
  `type` tinyint(1) unsigned NOT NULL default '0',
  `tpl` tinyint(4) unsigned NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `uid` (`uid`),
  KEY `type` (`type`)
) ENGINE=MyISAM {$extend};";

$sql[] = "DROP TABLE IF EXISTS `uchome_app_love_renting`;";
$sql[] = "
CREATE TABLE IF NOT EXISTS `uchome_app_love_renting` (
  `uid` int(10) unsigned NOT NULL default '0',
  `fuid` int(10) unsigned NOT NULL default '0',
  `id` int(11) unsigned NOT NULL default '0',
  `hash` int(10) unsigned NOT NULL default '0',
  `op` varchar(10) NOT NULL default '',
  `pay_type` tinyint(1) unsigned NOT NULL default '0',
  `dateline` int(10) unsigned NOT NULL default '0',
  UNIQUE KEY `uid` (`uid`,`fuid`,`id`,`op`)
) ENGINE=MyISAM {$extend};";

$sql[] = "DROP TABLE IF EXISTS `uchome_app_love_save`;";
$sql[] = "
CREATE TABLE IF NOT EXISTS `uchome_app_love_save` (
  `sid` int(10) unsigned NOT NULL auto_increment,
  `hid` int(10) unsigned NOT NULL default '0',
  `uid` int(10) unsigned NOT NULL default '0',
  `status` tinyint(1) unsigned NOT NULL default '0',
  `hash` int(10) unsigned NOT NULL default '0',
  PRIMARY KEY  (`sid`),
  UNIQUE KEY `uid` (`uid`,`hid`)
) ENGINE=MyISAM {$extend};";

$sql[] = "DROP TABLE IF EXISTS `uchome_app_love_user`;";
$sql[] = "
CREATE TABLE IF NOT EXISTS `uchome_app_love_user` (
  `uid` int(10) unsigned NOT NULL default '0',
  `username` varchar(20) NOT NULL default '',
  `sex` tinyint(1) unsigned NOT NULL default '1',
  `money` int(10) unsigned NOT NULL default '0',
  `house_num` tinyint(4) unsigned NOT NULL default '0',
  `love_num` int(10) unsigned NOT NULL default '0',
  `user_status` tinyint(1) unsigned NOT NULL default '0',
  `work_status` tinyint(1) NOT NULL default '-1',
  `live_id` int(10) unsigned NOT NULL default '0',
  `thick` int(10) unsigned NOT NULL default '0',
  `hid` int(10) unsigned NOT NULL default '0',
  UNIQUE KEY `uid` (`uid`),
  KEY `live_id` (`live_id`)
) ENGINE=MyISAM {$extend};";

$sql[] = "DROP TABLE IF EXISTS `uchome_app_love_user_1`;";
$sql[] = "
CREATE TABLE IF NOT EXISTS `uchome_app_love_user_1` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `uid` int(10) unsigned NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `uid` (`uid`)
) ENGINE=MyISAM {$extend};";

$sql[] = "DROP TABLE IF EXISTS `uchome_app_love_user_2`;";
$sql[] = "
CREATE TABLE IF NOT EXISTS `uchome_app_love_user_2` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `uid` int(10) unsigned NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `uid` (`uid`)
) ENGINE=MyISAM {$extend};";

$sql[] = "DROP TABLE IF EXISTS `uchome_app_love_wall`;";
$sql[] = "
CREATE TABLE IF NOT EXISTS `uchome_app_love_wall` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `uid` int(10) unsigned NOT NULL default '0',
  `name` varchar(20) default '',
  `title` varchar(30) NOT NULL default '',
  `message` varchar(250) NOT NULL default '',
  `color` tinyint(1) unsigned NOT NULL default '0',
  `icon` tinyint(4) unsigned NOT NULL default '0',
  `dateline` int(10) unsigned NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `uid` (`uid`)
) ENGINE=MyISAM {$extend};";

$sql[] = "DROP TABLE IF EXISTS `uchome_app_love_work`;";
$sql[] = "
CREATE TABLE IF NOT EXISTS `uchome_app_love_work` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `uid` int(10) unsigned NOT NULL default '0',
  `type` tinyint(1) unsigned NOT NULL default '0',
  `dateline` int(10) unsigned NOT NULL default '0',
  `status` tinyint(1) unsigned NOT NULL default '0',
  `has_pay` tinyint(1) unsigned NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `uid` (`uid`,`type`,`status`)
) ENGINE=MyISAM {$extend};";

$sql[] = "DROP TABLE IF EXISTS `uchome_app_love_miyu`;";
$sql[] = "
CREATE TABLE IF NOT EXISTS `uchome_app_love_miyu` (
  `mid` INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `hid` INTEGER(10) UNSIGNED NOT NULL DEFAULT '0',
  `uid` INTEGER(10) UNSIGNED NOT NULL DEFAULT '0',
  `username` VARCHAR(20) NOT NULL DEFAULT '',
  `message` TEXT NOT NULL,
  `dateline` INTEGER(10) UNSIGNED NOT NULL DEFAULT '0',
  PRIMARY KEY (`mid`),
  KEY `hid` (`hid`)
)ENGINE=MyISAM {$extend};";

foreach($sql as $val){
	$_SC['tablepre'] != 'uchome_' && $val = str_replace("`uchome_", '`'. $_SC['tablepre'], $val);
	$val = siconv($val, $_SC['charset'], 'utf8');
	query_exec($val);
}

$users = query_all("select s.uid, s.username, f.sex from ".tname("space")." as s , ".tname("spacefield")." as f  where f.uid = s.uid and  f.sex > 0 and s.avatar > 0");

foreach($users as $user){
		$data = array(
			'uid'=>$user['uid'],
			'username'=>addslashes($user['username']),
			'sex'=>$user['sex'],
			'money'=>$house_config['money'],
			'house_num'=>1,
			'love_num'=>0,
			'user_status'=>0,
			'work_status'=>-1,
			'live_id'=>0,
			'hid'=>0
		);
		inserttable('app_love_user', $data, false);
		$param = array('uid'=>$user['uid']);
		inserttable('app_love_user_' . $user['sex'], $param, false);
		$param = array(
			'cid'=>1,
			'uid'=>$user['uid'],
			'name'=> $house_kind[1],
			'status'=>1,
			'price'=>$house_db[1]['buy'] * 0.1
		);
		inserttable('app_love_house', $param, false);
}

swritefile('data/house_install.lock', $_SERVER['REQUEST_TIME']);
showmessage('恭喜你，安装成功！', 'house.php', 2);
?>